class SRShotgunFire extends KFShotgunFire;

var const float DamageStaminaExpenceRate;
var bool bSilentWeapon;
var float InvisExpence;

function DoFireEffect()
{
	local Vector StartProj, StartTrace, X,Y,Z;
    local Rotator R, Aim;
    local Vector HitLocation, HitNormal;
    local Actor Other;
    local int p;
    local int SpawnCount;
    local float theta;

	SRHumanPawn(Instigator).ConsumeStamina(WeaponStaminaCost());

	if ( !bSilentWeapon /*|| !SRPlayerReplicationInfo(Instigator.PlayerReplicationInfo).bInvisible*/ )
	{
		Instigator.MakeNoise(1.0);
		if ( SRHumanPawn(Instigator).bInvisible )
		{
			SRHumanPawn(Instigator).BecomeVisible();
		}
	}
	else
	{
		if ( SRHumanPawn(Instigator).bInvisible )
		{
			//DKGameType(Level.Game).ConsumeInvis(SRHumanPawn(Instigator).InvisController,InvisExpence);
		}
	}

    Weapon.GetViewAxes(X,Y,Z);

    StartTrace = Instigator.Location + Instigator.EyePosition();// + X*Instigator.CollisionRadius;
    StartProj = StartTrace + X*ProjSpawnOffset.X;
    if ( !Weapon.WeaponCentered() && !KFWeap.bAimingRifle )
	    StartProj = StartProj + Weapon.Hand * Y*ProjSpawnOffset.Y + Z*ProjSpawnOffset.Z;

    // check if projectile would spawn through a wall and adjust start location accordingly
    Other = Weapon.Trace(HitLocation, HitNormal, StartProj, StartTrace, false);

// Collision attachment debugging
 /*   if( Other.IsA('ROCollisionAttachment'))
    {
    	log(self$"'s trace hit "$Other.Base$" Collision attachment");
    }*/

    if (Other != None)
    {
        StartProj = HitLocation;
    }

    Aim = AdjustAim(StartProj, AimError);

    SpawnCount = Max(1, ProjPerFire * int(Load));

    switch (SpreadStyle)
    {
    case SS_Random:
        X = Vector(Aim);
        for (p = 0; p < SpawnCount; p++)
        {
            R.Yaw = Spread * (FRand()-0.5);
            R.Pitch = Spread * (FRand()-0.5);
            R.Roll = Spread * (FRand()-0.5);
            SpawnProjectile(StartProj, Rotator(X >> R));
        }
        break;
    case SS_Line:
        for (p = 0; p < SpawnCount; p++)
        {
            theta = Spread*PI/32768*(p - float(SpawnCount-1)/2.0);
            X.X = Cos(theta);
            X.Y = Sin(theta);
            X.Z = 0.0;
            SpawnProjectile(StartProj, Rotator(X >> Aim));
        }
        break;
    default:
        SpawnProjectile(StartProj, Aim);
    }

	if (Instigator != none && Instigator.Physics != PHYS_Falling)
		Instigator.AddVelocity(KickMomentum >> Instigator.GetViewRotation());
}

function float WeaponStaminaCost()
{
	local KFWeapon KFW;
	local float ExpenceMod;
	
	KFW = KFWeapon(Weapon);
	
	if ( KFW == none )
		return 0.0;
		
	ExpenceMod = 1.0;
	ExpenceMod *= FMax(1.0, ProjectileClass.default.DamageRadius/100.0);
	ExpenceMod *= Max(1,ProjPerFire);
	ExpenceMod *= Max(1,AmmoPerFire);
	if ( class<ShotgunBulletNew>(ProjectileClass) != none )
	{
		ExpenceMod *= class<KFWeaponDamageType>(ProjectileClass.default.MyDamageType).default.HeadShotDamageMult
					* class<ShotgunBulletNew>(ProjectileClass).default.HeadShotDamageMult;
		ExpenceMod *= 1.0 + 1.0 * class<SRVeterancy>(SRPlayerReplicationInfo(Instigator.Controller.PlayerReplicationInfo).ClientVeteranSkill).Static.GetShotgunPenetrationDamageMulti(SRPlayerReplicationInfo(Instigator.Controller.PlayerReplicationInfo),class<ShotgunBulletNew>(ProjectileClass).default.PenDamageReduction);
	}
	else if ( class<VSSDTBullet>(ProjectileClass) != none )
	{
		ExpenceMod *= class<KFWeaponDamageType>(ProjectileClass.default.MyDamageType).default.HeadShotDamageMult
					* class<VSSDTBullet>(ProjectileClass).default.HeadShotDamageMult * 1.5;
	}
	else if ( class<SVDcBullet>(ProjectileClass) != none )
	{
		ExpenceMod *= class<KFWeaponDamageType>(ProjectileClass.default.MyDamageType).default.HeadShotDamageMult
					* class<SVDcBullet>(ProjectileClass).default.HeadShotDamageMult * 2.0;
	}
	else if ( class<CrossbowArrowNew>(ProjectileClass) != none )
	{
		ExpenceMod *= class<KFWeaponDamageType>(ProjectileClass.default.MyDamageType).default.HeadShotDamageMult
					* class<CrossbowArrowNew>(ProjectileClass).default.HeadShotDamageMult * 2.0;
	}
	else if ( class<M99Bullet>(ProjectileClass) != none )
	{
		ExpenceMod *= class<KFWeaponDamageType>(ProjectileClass.default.MyDamageType).default.HeadShotDamageMult
					* class<M99Bullet>(ProjectileClass).default.HeadShotDamageMult * 2.0;
	}
	
	
	ExpenceMod *= class'SRVeterancy'.Static.AddDamage(SRPlayerReplicationInfo(Instigator.Controller.PlayerReplicationInfo),none,none,10000,ProjectileClass.default.MyDamageType) * 0.0001;
	
	if ( ThisModeNum == 0 || Bal_M4203AssaultRifle(KFW) != none || M79U(KFW) != none || M79FGrenadeLauncher(KFW) != none )
	{
		return ProjectileClass.default.Damage * ExpenceMod * DamageStaminaExpenceRate;
	}
	
	// ThisModeNum == 1, Our next actions ===>
	
/*
	if (  MP7MedicGun(KFW) != none || Bal_MP5MMedicGun(KFW) != none || WMediShot(KFW) != none
		|| M7A3MMedicGun(KFW) != none || HKMP7LLI(KFW) != none )
	{
		return ( class<DKHealProj>(ProjectileClass).defauHealBoostAmount + class<DKHealProj>(ProjectileClass).InstantHealAmount ) * ExpenceMod * DamageStaminaExpenceRate * 60.0;
	}
*/
	
	return 0.0;
}


event ModeDoFire()
{
	local float Rec;

	if (!AllowFire())
		return;

	Rec = GetFireSpeed();
	FireRate = default.FireRate/Rec;
	FireAnimRate = default.FireAnimRate*Rec;
	Rec = 1;
	Spread = GetSpread(Rec);

	if( !bFiringDoesntAffectMovement )
	{
		if (FireRate > 0.25)
		{
			Instigator.Velocity.x *= 0.1;
			Instigator.Velocity.y *= 0.1;
		}
		else
		{
			Instigator.Velocity.x *= 0.5;
			Instigator.Velocity.y *= 0.5;
		}
	}

	Super(BaseProjectileFire).ModeDoFire();

    // client
    if (Instigator.IsLocallyControlled())
    {
        HandleRecoil(Rec);
    }
}

function float GetSpread(out float Rec)
{
	local float ret;

	ret = default.Spread;
	
	if ( KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo) != none && KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo).ClientVeteranSkill != none )
	{
		ret *= KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo).ClientVeteranSkill.Static.ModifyRecoilSpread(KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo), self, Rec);
	}
	
	return ret;
}

defaultproperties
{
	DamageStaminaExpenceRate = 0.002;
	bSilentWeapon = false;
	InvisExpence = 0.00;
}
